//=================================================

var runtime;
var scene;
var model_md = rhAnnotationMetadataArray["x3d_model"];

//=================================================

function InitContext()
{
	var res = false;
	if (typeof(runtime) == "undefined")
		runtime = document.all.ev3de.runtime;
	if (typeof(scene) == "undefined")
		scene = document.all.ev3de.scene;
	res = (typeof(runtime) != "undefined") && (typeof(scene) != "undefined");
	if (res)
		res = (runtime != null) && (scene != null);
	return res;
}

//=================================================

function cbxChangeRM(r_mode)
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	var new_r_mode = scene.RENDER_MODE_DEFAULT;
	switch (r_mode)
	{
	case "illustration" :
		new_r_mode = scene.RENDER_MODE_ILLUSTRATION;
		break;
	case "shaded illustration" :
		new_r_mode = scene.RENDER_MODE_SHADED_ILLUSTRATION;
		break;
	case "solid" :
	default:
		new_r_mode = scene.RENDER_MODE_SOLID;
	}
	scene.renderMode = new_r_mode;
	scene.update();
}

//=== animation handling ==========================

var animationTimer;
var animationObject;
var animationIndex = 0;

//=================================================

function AnimObject(anim)
{
	this.animation = anim;
	this.frame_duration = 1 / anim.framesPerSecond;
	this.time_start = anim.startTime;
	this.time_end = anim.endTime;
	this.current_frame = 0;
	this.speed_factor = 1.0;
	this.mode_pingpong = false;
	this.mode_repeat = false;
	this.playing = false;
	this.paused = false;
}

function InitAnimation(anim_index)
{
	if ( (typeof(animationObject) == "undefined") || (animationIndex != anim_index) )
	{
		var anim = null;
		if ( (0 <= anim_index) && (anim_index < scene.animations.count) )
			anim = scene.animations.getByIndex(anim_index);
		if (anim)
		{
			scene.activateAnimation(anim);
			for (var back_idx = anim_index -1; back_idx > -1; back_idx --)
			{
				var b_anim = scene.animations.getByIndex(back_idx);
				b_anim.currentTime = b_anim.startTime;
			}
			animationObject = new AnimObject(anim);
			animationIndex = anim_index;
		}
	}
	if (typeof(animationTimer) == "undefined")
	{
		animationTimer = new document.all.ev3de.Creator.TimeEventHandler();
		animationTimer.onEvent = function(event)
		{
			if (!animationObject)
				return;
			if (animationObject.playing && !animationObject.paused)
			{
				animationObject.animation.currentTime += animationObject.frame_duration * animationObject.speed_factor;
    				if (animationObject.animation.currentTime >= animationObject.time_end)
				{
					if (animationObject.mode_repeat)
					{
						if (animationObject.mode_pingpong)
							animationObject.speed_factor *= -1;
						else
							animationObject.animation.currentTime = animationObject.time_start;
					}
					else
						animationObject.playing = false;
				}
				if (animationObject.animation.currentTime <= animationObject.time_start)
				{
					if (animationObject.mode_pingpong)
						animationObject.speed_factor *= -1;
				}
			}
		}
		runtime.addEventHandler(animationTimer);
	}
}

function btPlay()
{

	if (InitContext() )
	{
		InitAnimation(animationIndex);
		if (animationObject)
		{
			animationObject.playing = true;
			animationObject.paused = false;
		}
	}
	else
		alert("Context initialization failed!");

}

function btPause()
{
	if (animationObject)
		animationObject.paused = !animationObject.paused;
}

function btRewind()
{
	if (animationObject)
	{
		animationObject.playing = false;
		animationObject.paused = false;
		animationObject.animation.CurrentTime = animationObject.time_start;
	}
}

//=================================================

var mouseHandler;
var selectedNode = undefined;

//=================================================

function InitHandlers()
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	mouseHandler = new document.all.ev3de.Creator.MouseEventHandler();
	mouseHandler.onMouseDown = true;
	mouseHandler.onEvent = function(event)
	{
		if (event.isMouseDown)
		{
			selectedNode = undefined;
			var node = event.hits.target;
			if (node)
			{
				var obj = null;
				for (idx = 0; idx < model_md.objects.length; idx ++)
				{
					obj = model_md.objects[idx];
					if (obj.name == node.name)
					{

						selectedNode = node;
						break;
					}
				}
				selectedNode = node; // we dublicate this selection
			}
		}
		return true;
	}
	runtime.addEventHandler(mouseHandler);
}

//=================================================

function btHideSelected()
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	if (typeof(selectedNode) != "undefined")
		selectedNode.visible = false;
	scene.update();
}

function btHideUnselected()
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	if (typeof(selectedNode) != "undefined")
	{
		selectedNode.visible = true;
		for (idx = 0; idx < scene.nodes.count; idx ++)
		{
			var node = scene.nodes.getByIndex(idx);
			if (selectedNode != node)
				node.visible = false;
		}
	}
	scene.update();
}

function btShowAll()
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	for (idx = 0; idx < scene.nodes.count; idx ++)
	{
		var node = scene.nodes.getByIndex(idx);
		node.visible = true;
	}
	scene.update();
}

//=================================================

function InitASeqs()
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	while (0 < document.all.l_aseq.options.length)
	{
		document.all.l_aseq.options.remove(0);
	}
	for (var i = 0; i < model_md.ranges.length; ++i)
	{
		var range = model_md.ranges[i];
		var option = document.createElement("OPTION");
		option.text = range.name;
		option.value = i;
		document.all.l_aseq.add(option);
	}
}

function lClickASeq(aseq_idx)
{
	if (InitContext() )
	{
		btRewind();
		InitAnimation(aseq_idx);
	}
	scene.update();
}

//=================================================

function InitLayers()
{
	if (!InitContext() )
	{
		alert("context is not initiated!");
		return;
	}
	while (0 < document.all.l_layers.options.length)
	{
		document.all.l_layers.options.remove(0);
	}
	for (var i = 0; i < model_md.layers.length; ++i)
	{
		var lay = model_md.layers[i];
		var option = document.createElement("OPTION");
		option.text = lay.name;
		option.value = i;
		document.all.l_layers.add(option);
	}
}

function lClickLayer(lay_idx)
{
	var lay = model_md.layers[lay_idx];
	if (typeof(lay) != "undefined")
	{
		for (var idx = 0; idx < scene.nodes.count; idx ++)
		{
			var node = scene.nodes.getByIndex(idx);
			node.visible = false;
		}
		for (var idx = 0; idx < lay.object_names.length; idx ++)
		{
			var name = lay.object_names[idx];
			var node = scene.nodes.getByName(name);
			if (typeof(node) != "undefined")
				node.visible = true;
		}
	}
	scene.update();
}

//=================================================
